package com.sinog.biz.util;

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @desc 数据源工具类
 * @author lzq
 * @date 2022/11/8 13:22
 */
@Slf4j
public class DataSourceCheckUtils {

    /**
     * 检查数据源是否可用
     * @param jdbcTemplates jdbcTemplates
     */
    public static boolean checkRunningStatus(JdbcTemplate... jdbcTemplates) {
        for(JdbcTemplate jdbcTemplate : jdbcTemplates) {
            HikariDataSource dataSource = (HikariDataSource)jdbcTemplate.getDataSource();
            if(null == dataSource) {
                log.error("数据源{}不可用",dataSource);
                return false;
            }
            boolean running = dataSource.isRunning();
            boolean closed = dataSource.isClosed();
            if(!running) {
                String jdbcUrl = dataSource.getJdbcUrl();
                log.error("数据源{}不可用",jdbcUrl);
                return running;
            }
        }
        return true;
    }
}